// pages/mine/mine.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		dataInfo: {},
		numList: {
			total: '',
			subscribe: '',
			unsubscribe: '',
			unsubscriberate: ''
		}
  },

	// 切换在线状态的事件
	changeOnlineStatus: function () {
		var that = this
		wx.showActionSheet({
			itemList: ['在线', '忙碌', '离线'],
			success(res) {
				console.log(res.tapIndex)
				that.setOnlineStatus(res.tapIndex)
			},
			fail(res) {
				console.log(res.errMsg)
			}
		})
	},

	setOnlineStatus: function (index) {
		var that = this
		console.log(index)
		// 设置在线的请求
		if (index === 0) {
			wx.request({
				url: app.globalData.baseUrl + '/api/login/online',
				method: 'GET',
				data: {
					id: that.data.dataInfo.user_info.id
				},
				success: function (res) {
					if (res.data.status === 1) {
						wx.showToast({
							title: '设置成功',
							icon: 'success',
							duration: 1000
						})
						that.data.dataInfo.user_info.type = 1
						that.setData({
							dataInfo: that.data.dataInfo
						})
						wx.setStorage({
							key: "islogin",
							data: that.data.dataInfo
						})
					} else {
						wx.showToast({
							title: '操作失败',
							icon: 'none',
							duration: 1000
						})
					}
				}
			})
		} else if (index === 1) {
			// 设置忙碌的请求
			wx.request({
				url: app.globalData.baseUrl + '/api/login/busy',
				method: 'GET',
				data: {
					id: that.data.dataInfo.user_info.id
				},
				success: function (res) {
					if (res.data.status === 1) {
						wx.showToast({
							title: '设置成功',
							icon: 'success',
							duration: 1000
						})
						that.data.dataInfo.user_info.type = 2
						that.setData({
							dataInfo: that.data.dataInfo
						})
						wx.setStorage({
							key: "islogin",
							data: that.data.dataInfo
						})
					} else {
						wx.showToast({
							title: '操作失败',
							icon: 'none',
							duration: 1000
						})
					}
				}
			})
		} else if (index === 2) {
			// 设置离线的请求
			wx.request({
				url: app.globalData.baseUrl + '/api/login/outline',
				method: 'GET',
				data: {
					id: that.data.dataInfo.user_info.id
				},
				success: function (res) {
					if (res.data.status === 1) {
						wx.showToast({
							title: '设置成功',
							icon: 'success',
							duration: 1000
						})
						that.data.dataInfo.user_info.type = 3
						that.setData({
							dataInfo: that.data.dataInfo
						})
						wx.setStorage({
							key: "islogin",
							data: that.data.dataInfo
						})
					} else {
						wx.showToast({
							title: '操作失败',
							icon: 'none',
							duration: 1000
						})
					}
				}
			})
		}
	},

	//点击设置离线回复

	clickOfflineReply: function () {
		wx.navigateTo({
			url: '../offline/offline'
		})
	},

	// 切换账号
	changeAccount: function () {
		wx.navigateTo({
			url: '../login/login'
		})
	},

	// 退出登录
	quitLogin: function () {
		wx.navigateTo({
			url: '../login/login'
		})
	},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		var that = this
		wx.setNavigationBarTitle({
			title: '我的'
		})

		// 获取缓存里面的客服信息
		try {
			var value = wx.getStorageSync('islogin')
			if (value) {
				console.log(value)
				// 如果缓存里面有登录信息则发送请求
				this.setData({
					dataInfo: value
				})
			} else {
				// wx.navigateTo({
				// 	url: '../login/login'
				// })
			}
		} catch (e) {
			console.log(e)
		}

		// 获取客服对接的全部用户
		wx.request({
			url: app.globalData.baseUrl +  '/api/csuser/getalluser',
			method: 'Get',
			data: {
				butt_id: this.data.dataInfo.user_info.id
			},
			success: function (res) {
				var data = res.data.msg
				var subscribe = 0
				var unsubscribe = 0
				res.data.msg.forEach(function (item) {
					if (item.subscribe === 1) {
						subscribe++
					} else if (item.subscribe === 0) {
						unsubscribe++
					}
				})
				that.setData({
					numList: {
						total: res.data.msg.length,
						subscribe: subscribe,
						unsubscribe: unsubscribe,
						unsubscriberate: (unsubscribe / res.data.msg.length * 100).toFixed(2)
					}
				})
			}
		})

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})